package com.wcr.school.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcr.school.pojo.Admin;
import com.wcr.school.pojo.Clazz;
import com.wcr.school.pojo.Teacher;
import com.wcr.school.service.AdminService;
import com.wcr.school.util.MD5;
import com.wcr.school.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sms/adminController")
public class AdminController {
    @Autowired
    AdminService adminService;
    @GetMapping("/getAllAdmin/{pageNo}/{pageSize}")
    public Result getAllAdmin(
            @PathVariable("pageNo") Integer pageNo,
            @PathVariable("pageSize") Integer pageSize,
            String adminName
    )
    {
        //分页，带条件查询
        Page<Admin> page = new Page<>(pageNo,pageSize);
        //通过服务层
        IPage<Admin> iPage = adminService.getAdminByOption(page,adminName);
        //封装return返回
        return Result.ok(iPage);

    }


    @PostMapping("/saveOrUpdateAdmin")
    public Result addOrUpdateAdmin(@RequestBody Admin admin){
        Integer id = admin.getId();
        if(id==null||0==id){
            admin.setPassword(MD5.encrypt(admin.getPassword()));
        }
        adminService.saveOrUpdate(admin);
        return Result.ok();
    }
    @DeleteMapping("/deleteAdmin")
    public Result deleteTeacher(@RequestBody List<Integer> list){
        adminService.removeByIds(list);
        return Result.ok();
    }
}
